import request from "../../utils/request.js";
const themeManager = require("../../utils/theme.js");

// pages/settings/settings.js
Page(themeManager.enhancePage({
  data: {
    userInfo: {},
    cacheSize: "12.5 MB",
    currentTheme: 'dark', // 添加主题状态
  },

  // 加载用户信息
  loadUserInfo: function () {
    request.get("/users/my_profile").then((res) => {
      this.setData({
        userInfo: res.data || {},
      });
      this.setData({
        "userInfo.nickName": wx.getStorageSync("nickname") || "",
        "userInfo.avatarUrl": wx.getStorageSync("avatar") || "",
      });
      console.log("加载用户信息:", this.data.userInfo);
    });
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;

    // 更新页面数据
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
    });

    // 保存到本地存储
    try {
      wx.setStorageSync("avatar", avatarUrl);

      wx.showToast({
        title: "头像更新成功",
        icon: "success",
        duration: 2000,
      });
    } catch (error) {
      console.error("保存头像失败:", error);
      wx.showToast({
        title: "保存头像失败",
        icon: "error",
        duration: 2000,
      });
    }
  },

  onNicknameChange(e) {
    const nickName = e.detail.value;

    // 更新页面数据
    this.setData({
      "userInfo.nickName": nickName,
    });

    // 保存到本地存储
    try {
      wx.setStorageSync("nickname", nickName);
    } catch (error) {
      console.error("保存昵称失败:", error);
    }
  },

  onLoad: function (options) {
    this.loadUserInfo();
    // this.getUserAvatar();
    this.updateCacheSize();
  },

  updateCacheSize: function () {
    const res = wx.getStorageInfoSync();
    const sizeKB = res.currentSize;
    let sizeStr = "";
    if (sizeKB >= 1024) {
      sizeStr = (sizeKB / 1024).toFixed(2) + " MB";
    } else {
      sizeStr = sizeKB + " KB";
    }
    this.setData({
      cacheSize: sizeStr,
    });
  },

  // 清除缓存
  clearCache: function () {
    wx.showModal({
      title: "清除缓存",
      content:
        "确定要清除所有缓存数据吗？这将删除所有本地存储的数据，并导致您被退出登录。",
      success: (res) => {
        if (res.confirm) {
          // 清除本地存储
          wx.clearStorageSync();

          wx.showToast({
            title: "缓存已清除",
            icon: "success",
          });

          wx.navigateTo({
            url: "/pages/login/login",
          });
        }
      },
    });
  },

  // 显示关于我们
  showAbout: function () {
    wx.showModal({
      title: "AeroSentinel 智能环境检测系统",
      content:
        "一个专业的物联网环境监测解决方案，为您提供实时、准确的环境数据监控服务。",
      showCancel: false,
      confirmText: "知道了",
    });
  },

  toNewTicket: function () {
    wx.navigateTo({
      url: "/pages/create_ticket/tickets?operation=2",
    });
  },

  // 跳转到系统偏好设置
  toPreferences: function () {
    wx.navigateTo({
      url: "/pages/preferences/preferences",
    });
  },

  // 退出登录
  logout: function () {
    wx.showModal({
      title: "退出登录",
      content: "确定要退出登录吗？",
      success: (res) => {
        if (res.confirm) {
          // 清除登录状态
          wx.removeStorageSync('token');
          
          wx.showToast({
            title: "已退出登录",
            icon: "success",
            duration: 1500
          });

          // 跳转到登录页面
          setTimeout(() => {
            wx.reLaunch({
              url: "/pages/login/login"
            });
          }, 1500);
        }
      }
    });
  },

  // 页面分享
  onShareAppMessage: function () {
    return {
      title: "环境监测系统",
      path: "/pages/index/index",
    };
  },
}));
